package com.chaopei.znpk.controller;

import com.alibaba.fastjson.JSON;
import com.chaopei.znpk.entity.Classroom;
import com.chaopei.znpk.mapper.*;
import com.chaopei.znpk.service.TeacherService;
import com.chaopei.znpk.tools.Class_Course;
import com.chaopei.znpk.tools.Sksj;
import com.chaopei.znpk.tools.Teacher_Course;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author yws
 * @Date 2022/5/18 9:58
 */
@CrossOrigin
@RestController
public class PkController {
    @Resource
    ClassCoursesMapper class_coursesMapper;
    @Resource
    TeacherCoursesMapper teacherCoursesMapper;
    @Resource
    TeacherService teacherService;
    @Resource
    TeacherMapper teacherMapper;
    @Resource
    ClassroomMapper classroomMapper;
    @Resource
    SksjMapper sksjMapper;

    @RequestMapping("/pk")
    public String pk() {
        sksjMapper.deleteAll();
        classroomMapper.queryClassroomAll();
        teacherMapper.queryTeacherAll();
        Class_Course[] class_courses = class_coursesMapper.selectClassCourse();
        Map<String, String> map = new HashMap<>();
        List<Sksj> list = new ArrayList<>();
        for (Class_Course classCourse : class_courses) {
            Teacher_Course[] teacher_courses1 = teacherCoursesMapper.selectTeacherCourses(classCourse.getCourse());
            System.out.println(teacher_courses1.length+"我是长度");
            for (int i = 0; i < teacher_courses1.length; i++) {
                System.out.println(teacher_courses1[i].toString()+"我是不是正确的信息");
            }
            if (teacher_courses1.length > 0) {
                Teacher_Course teacher_courses = teacher_courses1[0];
                Integer fenpei = teacher_courses.getFenpei() + 1;
                /*班级与教师已对应*/
                teacherService.queryTeacherFenpei(teacher_courses.getTeacherId(), fenpei);

                Classroom[] classrooms = classroomMapper.selectClassroom(classCourse.getClassCount());//获取教室信息
                for (Classroom classroom : classrooms) {
                    String Classroot_time = classroom.getCid() + teacher_courses.getExpectTime();
                    String Teacher_time = teacher_courses.getTeacherId() + teacher_courses.getExpectTime();
                    if (!map.containsKey(Classroot_time) && !map.containsKey(Teacher_time)) {
                        //插入对应的课程信息
                        map.put(Classroot_time, Classroot_time);
                        map.put(Teacher_time, Teacher_time);
                        Sksj sksj = new Sksj(classCourse.getClassId(), classCourse.getClassName(),
                                classCourse.getCourse(), classCourse.getCName(),
                                teacher_courses.getTeacherId(), teacher_courses.getTeacherName(),
                                classroom.getCid(), classroom.getAddress(), teacher_courses.getExpectTime());
                        list.add(sksj);
                        classroomMapper.queryClassroomFenpei(classroom.getCid(), classroom.getFenpei() + 1);
                        break;
                    } else {
                        System.out.println(teacher_courses.getExpectTime()+"我就是时间");
                        //换时间插入
                        int shijian = Integer.parseInt(teacher_courses.getExpectTime());
                        int x = shijian / 10;
                        int y = shijian % 10;
                        for (int i = 0; i < 20; i++) {
                            if (y < 5) {
                                y += 1;
                            } else if (y == 5) {
                                if (x < 5) {
                                    x += 1;
                                } else if (x == 5) {
                                    x = 1;
                                    y = 1;
                                }
                            }
                        }
                        String Classroot_time1 = classroom.getCid() + (x * 10 + y + "");
                        String Teacher_time1 = teacher_courses.getTeacherId() + (x * 10 + y + "");
                        if (!map.containsKey(Classroot_time1) && !map.containsKey(Teacher_time1)) {
                            //插入对应的课程信息
                            map.put(Classroot_time1, Classroot_time1);
                            map.put(Teacher_time1, Teacher_time1);
                            Sksj sksj = new Sksj(classCourse.getClassId(), classCourse.getClassName(),
                                    classCourse.getCourse(), classCourse.getCName(),
                                    teacher_courses.getTeacherId(), teacher_courses.getTeacherName(),
                                    classroom.getCid(), classroom.getAddress(), (x * 10 + y + ""));
                            list.add(sksj);
                            classroomMapper.queryClassroomFenpei(classroom.getCid(), classroom.getFenpei() + 1);
                            break;
                        }
                    }
                }

            } else {
                return classCourse.getCName() + "没有对应的教师教授对应的课程";
            }
        }
        list.forEach(System.out::println);
        sksjMapper.insertSksj(list);
        return "排课完毕";
    }

    @RequestMapping("/selectClassKb")
    public String selectClassKb(String name) {
        System.out.println(" 以及 "+name);
        List<Sksj> list = sksjMapper.selectClassKb(name);
        //System.out.println(string);
        List<List<Sksj>> lists = selectPk(list);
        String string = JSON.toJSONString(lists);
        return string;
    }
    @RequestMapping("/selectTeacherKb")
    public String selectTeacherKb(String name) {
        List<Sksj> list = sksjMapper.selectTeacherKb(name);
        List<List<Sksj>> lists = selectPk(list);
        String string = JSON.toJSONString(lists);
        return string;
    }
    @RequestMapping("/selectCidKb")
    public String selectCidKb(String name) {
        List<Sksj> list = sksjMapper.selectCidKb(name);
        List<List<Sksj>> lists = selectPk(list);
        String string = JSON.toJSONString(lists);
        return string;
    }

    public List<List<Sksj>> selectPk(List<Sksj> list){
        Sksj [][]sksj = new Sksj[5][7];
        Collections.sort(list, new Comparator<Sksj>() {
            @Override
            public int compare(Sksj s1,Sksj s2){
                int temp1 = Integer.parseInt(s1.getSktime());
                int temp2 = Integer.parseInt(s2.getSktime());
                if(temp1>temp2)
                    return 1;
                else if (temp1<temp2)
                    return -1;
                else
                    return 0;
            }
        });
        for (Sksj sk:list) {
            int temp1 = Integer.parseInt(sk.getSktime())/10;
            int temp2 = Integer.parseInt(sk.getSktime())%10;
            sksj[temp1-1][temp2-1]=sk;
        }
       /* for (int i = 0; i < sksj.length; i++) {
            for (int j = 0; j < sksj[i].length; j++) {
                if (sksj[i][j]==null)
                    sksj[i][j] = new Sksj();
                System.out.println(sksj[i][j]);
            }
        }*/
        List<List<Sksj>> lists = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            List<Sksj> sksjList = new ArrayList<>();
            for (int j = 0; j < 7; j++) {
                sksjList.add(sksj[i][j]);
            }
            lists.add(sksjList);
        }
        return lists;
    }


}
